<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function test(){
		$list = D("FoodCategory")->select();
		var_dump($list);
	}
    public function index(){
    	$list = M("User")->order('id desc')->select();
    	$pageSize = 8;
    	$count = count($list)/$pageSize;
    	for($i=0;$i<$count;$i++){
    		$page[] = array_slice($list, $i*$pageSize,$pageSize);
    	}
    	$this->assign("count",$count);
    	$this->assign("page",$page);
		$this->display();
    }
    public function enter(){
    	if(IS_POST){
    		$enter = D("Enter");
    		$data = $enter->create($_POST);
    		$result = D("Enter")->add($data);
    		if($result){
    			$this->success("提交申请成功","index");
    		}else{
    			$this->error("提交申请失败");
    		}
    	}else{
	    	$this->display();
    	}
    }
    public function login(){
    	if(IS_POST){
    		$staff = M('Staff');
    		$password = I('password');
    		$where['account'] = I('account');
    		$sta = $staff->where($where)->find();
    		if($sta){
    			if($sta['password']==md5($password)){
    				session('auth',$sta);
    				switch ($sta['status']){
    					case 1:
    						$this->success("欢迎{$sta['name']}管理员",__APP__."/Admin/Order/orderList");
    						break;
    					case 2:
    						$this->success("欢迎{$sta['name']}",__APP__."/Staff/Order/index");
    						break;
    				}
    			}else{
    				$this->error("账号或者密码错误");
    			}
    		}else{
    			$this->error("账号不存在");
    		}
    	}else{
    		$this->display();
    	}
    }
    public function logout(){
    	session_unset();
    	session_destroy();
    	$this->redirect('Home/Index/index');
    }
}